{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7f3fd4fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "可逆矩阵案例验证：\n",
      "SVD逆与Numpy逆的差异范数: 0.0 (接近0表示正确)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# ==============================================\n",
    "# 案例1：可逆矩阵的逆计算\n",
    "# ==============================================\n",
    "np.random.seed(42)  # 固定随机种子\n",
    "A = np.random.rand(3, 3)  # 生成随机3x3矩阵（几乎总是可逆的）\n",
    "\n",
    "# 使用SVD分解矩阵A\n",
    "# U: 左奇异向量，正交矩阵\n",
    "# s: 奇异值数组（降序排列）\n",
    "# Vh: 右奇异向量的共轭转置矩阵\n",
    "U, s, Vt = np.linalg.svd(A)\n",
    "\n",
    "# 构建奇异值对角矩阵的逆（由于A可逆，所有奇异值非零）\n",
    "Sigma_inv = np.diag(1.0 / s)\n",
    "\n",
    "# 通过SVD计算逆矩阵：A⁻¹ = V * Σ⁻¹ * U^T\n",
    "A_inv_svd = Vt.T @ Sigma_inv @ U.T\n",
    "\n",
    "# 使用numpy官方方法计算逆矩阵\n",
    "A_inv_np = np.linalg.inv(A)\n",
    "\n",
    "# 验证结果：计算两个逆矩阵的差异范数\n",
    "error = np.linalg.norm(A_inv_svd - A_inv_np)\n",
    "print(\"可逆矩阵案例验证：\")\n",
    "print(\"SVD逆与Numpy逆的差异范数:\",  round(error, 4), \"(接近0表示正确)\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6276c9bf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "秩亏矩阵案例验证：\n",
      "SVD伪逆与Numpy伪逆的差异范数: 0.0 (接近0表示正确)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# ==============================================\n",
    "# 案例2：秩亏矩阵的伪逆计算\n",
    "# ==============================================\n",
    "B = np.array([[1, 2, 3], \n",
    "              [4, 5, 6], \n",
    "              [7, 8, 9]])  # 该矩阵秩为2（行列式为0）\n",
    "\n",
    "# 进行SVD分解（full_matrices保持默认True）\n",
    "U_b, s_b, Vt_b = np.linalg.svd(B)\n",
    "\n",
    "# 处理奇异值：忽略小于阈值的奇异值\n",
    "threshold = 1e-10\n",
    "s_inv = np.zeros_like(s_b)\n",
    "for i in range(len(s_b)):\n",
    "    if s_b[i] > threshold:\n",
    "        s_inv[i] = 1 / s_b[i]  # 有效奇异值的倒数\n",
    "    else:\n",
    "        s_inv[i] = 0          # 截断无效奇异值\n",
    "\n",
    "# 构建奇异值逆矩阵\n",
    "Sigma_inv_b = np.diag(s_inv)\n",
    "\n",
    "# 计算伪逆：B⁺ = V * Σ⁺ * U^T\n",
    "B_pinv_svd = Vt_b.T @ Sigma_inv_b @ U_b.T\n",
    "\n",
    "# 使用numpy官方伪逆函数\n",
    "B_pinv_np = np.linalg.pinv(B)\n",
    "\n",
    "# 验证结果差异\n",
    "error_pinv = np.linalg.norm(B_pinv_svd - B_pinv_np)\n",
    "print(\"\\n秩亏矩阵案例验证：\")\n",
    "print(\"SVD伪逆与Numpy伪逆的差异范数:\",  round(error_pinv, 4), \"(接近0表示正确)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c48fb420",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
